package com.irdstudio.efp.e4a.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.e4a.service.dao.SRuleCollDtlDao;
import com.irdstudio.efp.e4a.service.dao.SRuleCollDtlParamDao;
import com.irdstudio.efp.e4a.service.domain.SRuleCollDtl;
import com.irdstudio.efp.e4a.service.facade.SRuleCollDtlService;
import com.irdstudio.efp.e4a.service.vo.SRuleCollDtlVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.SequenceConstance;

/**
 * <p>
 * ServiceImpl: 系统规则集明细表
 * <p>
 * 
 * @author common
 * @date 2018-05-03
 */
@Service("sRuleCollDtlService")
public class SRuleCollDtlServiceImpl extends AbstractFrameworkService implements SRuleCollDtlService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(SRuleCollDtlServiceImpl.class);

	@Autowired
	private SRuleCollDtlDao sRuleCollDtlDao;

	@Autowired
	private SRuleCollDtlParamDao sRuleCollDtlParamDao;

	/**
	 * 新增操作
	 */
	public int insertSRuleCollDtl(SRuleCollDtlVO inSRuleCollDtlVo) {
		logger.debug("当前新增数据为:" + inSRuleCollDtlVo.toString());
		int num = 0;
		try {
			SRuleCollDtl sRuleCollDtl = new SRuleCollDtl();
			String dtlId = this.getSequence(SequenceConstance.RULE_CFG_SEQ_ID);
			setInsertDefaultProperty(inSRuleCollDtlVo);
			inSRuleCollDtlVo.setDtlId(dtlId);
			beanCopy(inSRuleCollDtlVo, sRuleCollDtl);
			num = sRuleCollDtlDao.insertSRuleCollDtl(sRuleCollDtl);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SRuleCollDtlVO inSRuleCollDtlVo) {
		logger.debug("当前删除数据条件为:" + inSRuleCollDtlVo);
		int num = 0;
		try {
			SRuleCollDtl sRuleCollDtl = new SRuleCollDtl();
			beanCopy(inSRuleCollDtlVo, sRuleCollDtl);
			num = sRuleCollDtlDao.deleteByPk(sRuleCollDtl);
			sRuleCollDtlParamDao.deleteByRuleCfgId(sRuleCollDtl.getDtlId());
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRuleCollDtlVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(SRuleCollDtlVO inSRuleCollDtlVo) {
		logger.debug("当前修改数据为:" + inSRuleCollDtlVo.toString());
		int num = 0;
		try {
			SRuleCollDtl sRuleCollDtl = new SRuleCollDtl();
			beanCopy(inSRuleCollDtlVo, sRuleCollDtl);
			num = sRuleCollDtlDao.updateByPk(sRuleCollDtl);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRuleCollDtlVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public SRuleCollDtlVO queryByPk(SRuleCollDtlVO inSRuleCollDtlVo) {

		logger.debug("当前查询参数信息为:" + inSRuleCollDtlVo);
		try {
			SRuleCollDtl querySRuleCollDtl = new SRuleCollDtl();
			beanCopy(inSRuleCollDtlVo, querySRuleCollDtl);
			SRuleCollDtl queryRslSRuleCollDtl = sRuleCollDtlDao.queryByPk(querySRuleCollDtl);
			if (Objects.nonNull(queryRslSRuleCollDtl)) {
				SRuleCollDtlVO outSRuleCollDtlVo = beanCopy(queryRslSRuleCollDtl, new SRuleCollDtlVO());
				logger.debug("当前查询结果为:" + outSRuleCollDtlVo.toString());
				return outSRuleCollDtlVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollDtlVO> queryAllOwner(SRuleCollDtlVO sRuleCollDtlVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SRuleCollDtl sRuleCollDtl = beanCopy(sRuleCollDtlVo, new SRuleCollDtl());
		List<SRuleCollDtlVO> list = null;
		try {
			List<SRuleCollDtl> sRuleCollDtls = sRuleCollDtlDao.queryAllOwnerByPage(sRuleCollDtl);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sRuleCollDtls.size());
			pageSet(sRuleCollDtls, sRuleCollDtl);
			list = (List<SRuleCollDtlVO>) beansCopy(sRuleCollDtls, SRuleCollDtlVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollDtlVO> queryAllCurrOrg(SRuleCollDtlVO sRuleCollDtlVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SRuleCollDtl sRuleCollDtl = beanCopy(sRuleCollDtlVo, new SRuleCollDtl());
		List<SRuleCollDtl> sRuleCollDtls = sRuleCollDtlDao.queryAllCurrOrgByPage(sRuleCollDtl);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sRuleCollDtls.size());
		List<SRuleCollDtlVO> list = null;
		try {
			pageSet(sRuleCollDtls, sRuleCollDtl);
			list = (List<SRuleCollDtlVO>) beansCopy(sRuleCollDtls, SRuleCollDtlVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollDtlVO> queryAllCurrDownOrg(SRuleCollDtlVO sRuleCollDtlVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRuleCollDtl sRuleCollDtl = beanCopy(sRuleCollDtlVo, new SRuleCollDtl());
		List<SRuleCollDtl> sRuleCollDtls = sRuleCollDtlDao.queryAllCurrDownOrgByPage(sRuleCollDtl);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sRuleCollDtls.size());
		List<SRuleCollDtlVO> list = null;
		try {
			pageSet(sRuleCollDtls, sRuleCollDtl);
			list = (List<SRuleCollDtlVO>) beansCopy(sRuleCollDtls, SRuleCollDtlVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 当前根据规则集ID查询规则配置明细
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollDtlVO> querySRuleCollDtlAllByCollId(SRuleCollDtlVO sRuleCollDtlVo) {
		logger.debug("当前根据规则集ID：" + sRuleCollDtlVo.getRuleCollId() + "查询规则配置明细开始");
		SRuleCollDtl sRuleCollDtl = beanCopy(sRuleCollDtlVo, new SRuleCollDtl());
		List<SRuleCollDtl> sRuleCollDtls = sRuleCollDtlDao.querySRuleCollDtlAllByCollIdByPage(sRuleCollDtl);
		logger.debug("当前根据规则集ID：" + sRuleCollDtlVo.getRuleCollId() + "查询规则配置明细数量为:" + sRuleCollDtls.size());
		List<SRuleCollDtlVO> list = null;
		try {
			pageSet(sRuleCollDtls, sRuleCollDtl);
			list = (List<SRuleCollDtlVO>) beansCopy(sRuleCollDtls, SRuleCollDtlVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	/**
	 * 根据规则id新增
	 */
	public int insertsInitRuleAndParamByRuleIds(SRuleCollDtlVO sRuleCollDtlVo) {
		List<String> ids = Arrays.asList(sRuleCollDtlVo.getRuleId().split(","));
		logger.info("规则集ID：" + sRuleCollDtlVo.getRuleCollId() + "批量插入规则" + ids + "及其对应参数开始......");
		ids.stream().forEach(s -> s.trim());
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("seqId", SequenceConstance.RULE_CFG_SEQ_ID);
		paramMap.put("domain", sRuleCollDtlVo);
		paramMap.put("list", ids);
		int num = 0;
		try {
			List<SRuleCollDtl> ruleCollDtls = sRuleCollDtlDao.queryInsertsByRuleIdsCount(paramMap);
			if (ruleCollDtls != null && ruleCollDtls.size() > 0) {
				String dtlId;
				for (SRuleCollDtl dtl : ruleCollDtls) {
					dtlId = this.getSequence(SequenceConstance.RULE_CFG_SEQ_ID);
					dtl.setDtlId(dtlId);
				}

				paramMap.put("ruleCollDtls", ruleCollDtls);
				num = this.sRuleCollDtlDao.insertsByRuleIds(paramMap);
				SRuleCollDtl sRuleCollDtl = beanCopy(sRuleCollDtlVo, new SRuleCollDtl());
				this.sRuleCollDtlParamDao.insertByRuleIdsNonExists(sRuleCollDtl);
			}
			logger.info("规则集ID：" + sRuleCollDtlVo.getRuleCollId() + "批量插入规则" + ids + "及其对应参数结束......");
		} catch (Exception e) {
			logger.error("规则集ID：" + sRuleCollDtlVo.getRuleCollId() + "批量插入规则" + ids + "及其对应参数出现异常!", e);
			num = -1;
		}
		return num;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SRuleCollDtlVO> queryAllByCollId(String ruleCollId) {
		logger.debug("当前根据规则集ID：" + ruleCollId + "查询规则配置明细开始");
		List<SRuleCollDtlVO> list = null;
		try {
			List<SRuleCollDtl> sRuleCollDtls = sRuleCollDtlDao.queryAllByCollId(ruleCollId);
			list = (List<SRuleCollDtlVO>) beansCopy(sRuleCollDtls, SRuleCollDtlVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}
}
